package com.gui.jdbc.simple;

import lombok.extern.slf4j.Slf4j;

import java.sql.*;

@Slf4j
public class JDBCDemo {

    public static void main(String[] args) {

        String url = "jdbc:mysql://192.168.207.111:3306/test?serverTimezone=Asia/Shanghai&characterEncoding=utf-8&useSSL=false";
        String userName = "root";
        String password = "1qaz2wsx";
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            connection = DriverManager.getConnection(url, userName, password);
            statement = connection.createStatement();
            String sql = "SELECT * FROM genshin_role";
            resultSet = statement.executeQuery(sql);
            while (resultSet.next()) {
                log.debug("id = {}, name = {}, age = {}", resultSet.getInt("id"), resultSet.getString("name"), resultSet.getInt("age"));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            if (resultSet != null) {
                try {
                    resultSet.close();
                    System.out.println("释放资源-> resultSet");
                } catch (SQLException e) {
                    log.error("释放资源失败");
                }
            }
            if (statement != null) {
                try {
                    statement.close();
                    System.out.println("释放资源-> statement");
                } catch (SQLException e) {
                    log.error("释放资源失败");
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                    System.out.println("释放资源-> connection");
                } catch (SQLException e) {
                    log.error("释放资源失败");
                }
            }
        }


    }
}
